//
//  ResponseCode.swift
//  Inquiry
//
//  Created by SSD on 2019/9/3.
//  Copyright © 2019 SSD. All rights reserved.
//

import Foundation
struct ResponseCode {
    var code: Int
    fileprivate var _message: String
    var message: String {
        var prefix = ""
        if AppInfo.default.environment == .development {
            prefix = "\(code)"
        }
        let result = prefix + _message
        return result
    }
    init(code: Int, message: String? = nil) {
        self.code = code
        self._message = message ?? ResponseCode.getDefaultMessageWithCode(code)
    }
}
extension ResponseCode {
    static func system_error(message: String? = nil) -> ResponseCode {
        return ResponseCode(code: -20001, message: message)
    }
    static func server_error(message: String? = nil) -> ResponseCode {
        return ResponseCode(code: 500, message: message)
    }
    static func no_error(message: String? = nil) -> ResponseCode {
        return ResponseCode(code: 200, message: message)
    }
}
private extension ResponseCode {
    static func getDefaultMessageWithCode(_ code:Int) -> String {
        var result = "unknow error"
        switch code {
        case -20001:
            result = "system error"
        case 500:
            result = "server error"
        case 200:
            result = "success"
        default:
            break
        }
        return result
    }
}
extension ResponseCode: Equatable {
    static func == (lhs: ResponseCode, rhs: ResponseCode) -> Bool {
        return lhs.code == rhs.code
    }
}
